package com.cust.zy.newtask.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cust.zy.newtask.R;
import com.cust.zy.newtask.TaskApplication;
import com.cust.zy.newtask.model.Task;

/**
 * Created by zhuangyuan on 4/12/2017.
 * 邮箱：zhuangyuan.k@qq.com
 */

public class TaskActivity extends AppCompatActivity {

    private int exitApplicationCertain = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.today_task);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.mipmap.task_list);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TaskActivity.this, TaskListActivity.class);
                startActivity(intent);
                finish();
            }
        });
        LinearLayout amL = (LinearLayout) findViewById(R.id.today_task_am);
        LinearLayout pmL = (LinearLayout) findViewById(R.id.today_task_pm);
        amL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TaskActivity.this, KillWordsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(KillWordsActivity.AM_OR_PM, KillWordsActivity.AM);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        pmL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TaskActivity.this, KillWordsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(KillWordsActivity.AM_OR_PM, KillWordsActivity.PM);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_plan, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(exitApplicationCertain==0){
                Toast.makeText(TaskActivity.this,"再一次点击退出应用程序",Toast.LENGTH_SHORT).show();
                exitApplicationCertain++;
            }else{
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }
        return false;
    }
}
